#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2023, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #
ONE_LOCATION = ENV['ONE_LOCATION']

if !ONE_LOCATION
    RUBY_LIB_LOCATION = '/usr/lib/one/ruby'
    GEMS_LOCATION     = '/usr/share/one/gems'
    VMDIR             = '/var/lib/one'
    CONFIG_FILE       = '/var/lib/one/config'
else
    RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby'
    GEMS_LOCATION     = ONE_LOCATION + '/share/gems'
    VMDIR             = ONE_LOCATION + '/var'
    CONFIG_FILE       = ONE_LOCATION + '/var/config'
end

# %%RUBYGEMS_SETUP_BEGIN%%
if File.directory?(GEMS_LOCATION)
    real_gems_path = File.realpath(GEMS_LOCATION)
    if !defined?(Gem) || Gem.path != [real_gems_path]
        $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }

        # Suppress warnings from Rubygems
        # https://github.com/OpenNebula/one/issues/5379
        begin
            verb = $VERBOSE
            $VERBOSE = nil
            require 'rubygems'
            Gem.use_paths(real_gems_path)
        ensure
            $VERBOSE = verb
        end
    end
end
# %%RUBYGEMS_SETUP_END%%

$LOAD_PATH << RUBY_LIB_LOCATION

require 'CommandManager'
require 'rexml/document'
require 'base64'

require_relative '../../tm/lib/tm_action'

daction64 = STDIN.read
_ds_id    = ARGV[0]

# Image path in the form:
#   rsync://100/4/0:6da1c7,1:06132a,2:03fc2a/var/lib/one//datastores/100/13/6da1c7/disk.0.0
#
#   datastore_id = 100
#   backupjob id = 4 (can be empty)
#   last snap    = 03fc2a
#   base path    = /var/lib/one//datastores/100/13

begin
    rds = REXML::Document.new(Base64.decode64(daction64)).root.elements
    img = rds['/DS_DRIVER_ACTION_DATA/IMAGE/PATH'].text
    rsync_host = rds['/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/RSYNC_HOST'].text
    rsync_user = rds['/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/RSYNC_USER'].text

    img.slice! 'rsync://'

    parts  = img.split('/')
    diskid = parts[-1].match(/disk\.([0-9]+)/)

    base_path = "/#{parts[3..-3].join('/')}/"

    if !diskid
        STDERR.puts "Wrong format for disk filename #{base_path}"
        exit(-1)
    end

    last_snap = parts[2].split(',')[-1].split(':')[-1]

    cmd = "cat #{base_path}#{last_snap}/vm.xml"
rescue StandardError => e
    STDERR.puts e.message
    exit(-1)
end

rc = TransferManager::Action.ssh('gather_vm_xml',
                                 :host     => "#{rsync_user}@#{rsync_host}",
                                 :cmds     => cmd,
                                 :forward  => true,
                                 :nostderr => false,
                                 :nostdout => false)

if rc.code != 0
    STDERR.puts rc.stderr
    exit(-1)
end

vm = REXML::Document.new(Base64.decode64(rc.stdout)).root

# Done in two steps to support all Ruby versions
xpath = "/VM/TEMPLATE/DISK [ DISK_ID =  #{diskid[1]} ]"
disk  = vm.elements[xpath]

if !disk
    STDERR.puts "Cannot find disk #{diskid[1]} in VM backup info"
    exit(-1)
end

size = disk.elements['//SIZE']

if !size
    STDERR.puts "Cannot find size for disk  #{diskid[1]} in VM backup info"
    exit(-1)
end

STDOUT.puts size.text

exit(0)
